package com.ktjiaoyu.ktmall.dao.impl;

import com.ktjiaoyu.ktmall.dao.BaseDao;
import com.ktjiaoyu.ktmall.dao.CategoryDao;
import com.ktjiaoyu.ktmall.entity.Category;
import com.ktjiaoyu.ktmall.entity.NewsType;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CategoryDaoImpl extends BaseDao implements CategoryDao {
    @Override
    public List<Category> findAll() {
        String sql = "SELECT * FROM kt_category WHERE is_deleted = 0 ORDER BY parent_id,sort ASC";
        try {
            return executeQuery(sql,rs->{
                List<Category> list = new ArrayList<>();
                try {
                    while (rs.next()){
                        list.add(extrctCategory(rs));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return list;
            });
        } catch (SQLException e) {
           e.printStackTrace();
           return new ArrayList<>();
        }
    }

    private Category extrctCategory(ResultSet rs) throws SQLException {
        Category category = new Category();
        category.setId(rs.getString("id"));
        category.setName(rs.getString("name"));
        category.setParentId(rs.getString("parentId"));
        category.setIcon(rs.getString("icon"));
        category.setSort(rs.getInt("sort"));
        category.setStatus(rs.getInt("status"));
        category.setIsDeleted(rs.getInt("isDeleted"));
        return category;
    }
}
